<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Session;
use DB;
use Crypt;
class LoginController extends Controller
{
    public function login()
    {
        return view('admin.login.login');
    }
    //login
    public function dologin(Request $request)
    {
         $manage = DB::table('user') -> select('manage') -> where('email', $request -> email) -> first();


         $data = $request -> except('_token');
            if($manage){
                  if($manage -> manage == 1)
              {

                //dd($data);
                //检测验证码是否正确
                $code = Session::get('milkcaptcha');
                if($code !== $data['code'])
                {
                    return back() -> with(['info' =>'验证码不正确']);
                }
                $res = DB::table('user') -> where('email', $data['email']) -> first();
                //dd($res);
                //$password = $data['password'];
                $pwd = $res -> password;
                // dd($pwd);
                $pwd =  Crypt::decrypt($pwd);
                 // dd($pwd);

                if($pwd !== $data['password'])
                {
                    return back() -> with(['info' => '用户邮箱不正确或密码错误']);
                }
                 // $res = session(['manage' => $res]);
                 // dd($res);
                session(['master' => $res]);

                return redirect('admin/index/index') -> with(['info' => '登录成功']);


                 }else
                 {
                 //跳回首页
                 return back() -> with(['info' => '您还不是管理员']);
             }
         }else
         {
            return back() -> with(['info' => '不存在该用户']);
         }



}

    public function index()
    {
        return view('admin.index.index');
    }
    //logout
    public function logout()
    {
        Session::forget('master');
        return view('admin.login.login');
    }
}

